package com.ovhoo.vdm.vdm;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;



public class VDMLoader {
	
	
	private final static String apiurl = "http://api.betacie.com/view/";
	
    private final static String lastPosts = "http://api.betacie.com/view/last";
    private final static String randomPosts = "http://api.betacie.com/view/random";
    private final static String topPosts = "http://api.betacie.com/view/top";
    private final static String flopPosts = "http://api.betacie.com/view/flop";

    private static byte[] sBuffer = new byte[512];
    private static NetFileLoader fileLoader = new NetFileLoader();

    @SuppressWarnings("serial")
    public static class ApiException extends Exception {
        public ApiException(String detailMessage, Throwable throwable) {
            super(detailMessage, throwable);
        }

        public ApiException(String detailMessage) {
            super(detailMessage);
        }
    }
    
    public static ArrayList<VDMQuote> getLastPosts(String lng){
		// Query the API for content
        final String content = fileLoader.getUrlContent(lastPosts + "?language="+ lng + "&key=" + VDMPropertyManager.developprKey);
        
        if (content == null){
        	return null;
        }
        
        final ArrayList<VDMQuote> quotes = VDMParser.parseString(content);
        if (quotes != null) {
            return quotes;
        }
        else {
        	return null;
        }
            
	}
    
    public static ArrayList<VDMQuote> getRandomPosts(String lng){
		// Query the API for content
        final String content = fileLoader.getUrlContent(randomPosts + "?language="+ lng + "&key=" + VDMPropertyManager.developprKey);
        
        if (content == null){
        	return null;
        }
        
        final ArrayList<VDMQuote> quotes = VDMParser.parseString(content);
        if (quotes != null) {
            return quotes;
        }
        else {
        	return null;
        }      
	}
    
    public static ArrayList<VDMQuote> getTopPosts(String lng){
		// Query the API for content
        final String content = fileLoader.getUrlContent(topPosts + "?language="+ lng + "&key=" + VDMPropertyManager.developprKey);
        
        if (content == null){
        	return null;
        }
        
        final ArrayList<VDMQuote> quotes = VDMParser.parseString(content);
        if (quotes != null) {
            return quotes;
        }
        else {
        	return null;
        }
            
	}
	
    
    public static ArrayList<VDMQuote> getFlopPosts(String lng){
		// Query the API for content
        final String content = fileLoader.getUrlContent(flopPosts + "?language="+ lng + "&key=" + VDMPropertyManager.developprKey);
        
        if (content == null){
        	return null;
        }
        
        final ArrayList<VDMQuote> quotes = VDMParser.parseString(content);
        if (quotes != null) {
            return quotes;
        }
        else {
        	return null;
        }        
	}
    
    public static ArrayList<VDMQuote> getuoteByID(String lng, int id){
		// Query the API for content
        final String content = fileLoader.getUrlContent(apiurl + id + "?language="+ lng + "&key=" + VDMPropertyManager.developprKey);
        
        if (content == null){
        	return null;
        }
        
        final ArrayList<VDMQuote> quotes = VDMParser.parseString(content);
        if (quotes != null) {
            return quotes;
        }
        else {
        	return null;
        }      
	}

}
